﻿using HCMS.IServices;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;

namespace HCMS.Services
{
    /// <summary>
    /// 应用服务 基础
    /// </summary>
    public class CommonAppService<TDbContext, TEntity> : ApplicationService, ICommonAppService<TDbContext, TEntity> where TEntity : class, IEntity<int>
    {
        private readonly IRepository<TEntity> _baseRepository;

        public CommonAppService(
            IRepository<TEntity> baseRepository
            )
        {
            _baseRepository = baseRepository;
        }

        #region GetAllAsync 获取全部
        /// <summary>
        /// 获取全部
        /// </summary>
        /// <returns></returns>
        public async Task<IQueryable<TEntity>> GetAllAsync()
        {
            return await _baseRepository.GetQueryableAsync();
        }
        #endregion

        #region BaseFirstOrDefaultAsync 获取对象
        /// <summary>
        /// 获取对象
        /// </summary>
        /// <param name="expression">条件表达式</param>
        /// <returns></returns>
        public async Task<TEntity> BaseFirstOrDefaultAsync(Expression<Func<TEntity, bool>> expression)
        {
            return await _baseRepository.FirstOrDefaultAsync(expression);
        }
        #endregion

        #region BaseInsertAsync 添加
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public async Task<TEntity> BaseInsertAsync(TEntity entity)
        {
            // 记录日志

            var result = await _baseRepository.InsertAsync(entity);
            return result;
        }
        #endregion

        #region BaseUpdateAsync 更新
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public async Task<TEntity> BaseUpdateAsync(TEntity entity)
        {
            // 记录日志

            var result = await _baseRepository.UpdateAsync(entity);
            return result;
        }
        #endregion

    }
}
